package com.manhhavu.jemas.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Random;

import com.manhhavu.jemas.data.object.ClickedLink;

public class ClickedLinkDataManager {
	private static Random random = new Random();
	
	private ClickedLinkDataManager() {		
		
	}
		
	public static void addClickedLink(ClickedLink link) {
		try {
			Connection connection = MySQLAccess.getConnection();			
			PreparedStatement smt = connection.prepareStatement("INSERT INTO clicked_links(idc, link, datetime) VALUES(?,?,?)");						
			long id = Math.abs(random.nextLong()); 
			
			smt.setLong(1, id);
			smt.setString(2, link.getLink());
			smt.setDate(3, new java.sql.Date(link.getDatetime().getTime()));			
			smt.executeUpdate();			
			connection.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static List<ClickedLink> getAllClickedLink() {
		List<ClickedLink> links = null;
		
		try {
			Connection connection = MySQLAccess.getConnection();
			
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery("SELECT * FROM clicked_links");
			
			while (resultSet.next()) {
				long id = resultSet.getLong("idc");
				String l = resultSet.getString("link");
				
				// Fill in data
				
				System.out.println(id + ": " + l);
			}
			
			connection.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
}
